var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http)

app.get('/', function(req, res) {
  // res.send('<h1>Hello world</h1>');
  res.sendFile(__dirname + '/index.html')
});

app.get('/socket.io.js', function(req, res) {
  res.sendFile(__dirname + '/socket.io.js')
})

io.on('connection', function(socket) {
  io.emit('chat message', {some: 'welcome the new user', from: socket.id})
  socket.on('disconnect', function(){
    console.log('user disconnected');
    io.emit('chat message', {some: `The user gone`, from: socket.id})
  });
  socket.on('chat message', function(msg) {
    io.emit('chat message', {some: msg, from: socket.id})
  })
  // socket.on('chat message', function(msg) {
  //   console.log('message: ' + msg)
  //   // add the bellow together will send to every one include yourslef
  //   socket.broadcast.emit('chat message', {some: msg}) // send to the rest
  //   socket.emit('chat message', {some: msg}) // send to myself
  // })
})

http.listen(3000, function(){
  console.log('listening on *:3000');
});